package com.igroupes.ovineherd.model.Style;

import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.enums.FormKey;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class TextareaStyle extends InputStyle {

    public TextareaStyle(){

    }
    public TextareaStyle(String label, Object value, Boolean required, Integer maxLength, String desc) {
        this(label, value, required,maxLength);
        this.disabled = false;
        this.desc = desc;
    }
    private TextareaStyle(String label, Object value, Boolean required, Integer maxLength) {
        this(label, value, required);
        this.maxLength = maxLength;
    }
    public TextareaStyle(String label, Object value, Boolean required) {
        super(label, value, required);
        this.maxLength = 1000;
        this.disabled = false;
        this.type =  FormKey.TEXTAREA.getCode();
    }

    @Override
    public Boolean verify(Object o) {
        if(!(o instanceof TextareaStyle)){
            throw new ModelException("校验style类类型错误");
        }
        TextareaStyle style = (TextareaStyle) o;
        if(!style.getDisabled()){
            super.verify(o);
            String value = (String) style.getValue();
            if(style.getRequired() ){
                if(StringUtils.isBlank(value)){
                    throw new ModelException(style.getLabel()+"值是必传字段");
                }

            }
            if(StringUtils.isNotBlank(value)){
                if(!(style.getValue() instanceof String)){
                    throw new ModelException(style.getLabel()+"值必须是字符串");
                }

                if(0 < style.getMaxLength() && value.length() > style.getMaxLength()){
                    throw new ModelException(style.getLabel()+"值最大长度"+style.getMaxLength());
                }
            }
        }
        return true;
    }
}
